package jzOffer;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class Num40 {
    public int[] getLeastNumbers(int[] arr, int k) {
        if(arr.length==0 || k==0) return new int[0];
        Queue<Integer> q=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for(int value:arr){
            if(q.size()<k){
                q.offer(value);
            }else{
                if(value<q.peek()){
                    q.poll();
                    q.offer(value);
                }
            }
        }
        int[] ret=new int[q.size()];
        for(int i=0;i<ret.length;i++){
            ret[i]=q.poll();
        }
        return ret;
    }
}
